package com.mi.service.impl;

import com.mi.dao.UserDao;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mi.entity.User;
import com.mi.service.UserService;
import com.mi.util.NoteResult;

@Service
public class UserServiceImpl implements UserService{

	@Resource
	private UserDao dao; /*注入userDao到UserService bean工厂中*/

	/*登录逻辑判断*/
	public NoteResult LoginUser(String userName, String userPassword) {
		  System.out.println("service=====userName====="+userName);
		  User loginUser = dao.LoginUser(userName);
		  NoteResult result=new NoteResult();
		  if(loginUser==null){			
			  result.setStatus(1);
			  result.setMsg("此用户名不存在");		
				return result;
		  }
		 if(!loginUser.getUserPassword().equals(userPassword)){			  
			  result.setStatus(2);
			  result.setMsg("密码错误");
			  return result;
		  }	
			  result.setStatus(0);
			  result.setMsg("用户名和密码验证成功");
			  result.setData(loginUser);
			  return result;		
	
	}

	@Override/* 注册账号方法*/
	public NoteResult addUser(User user) {
		  NoteResult result=new NoteResult();
		       boolean b = dao.addUser(user);
		       if(b){
		    	   result.setStatus(0);
		    	   result.setMsg("注册成功");		    	   
		       }else {
				result.setStatus(1);
				result.setMsg("注册失败");
			}
			
		return result;
	}

	/*注册用户名表单校验*/
	public NoteResult UserGet(String userName) {
		   User loginUser = dao.LoginUser(userName);
		   NoteResult result=new NoteResult();
		   if(loginUser==null){
			   result.setStatus(0);
			   result.setMsg("OK");
		   }else {
			   result.setStatus(1);
			   result.setMsg("此用户名已存在");
		}
		
		return result;
	}

}
